package com.example.administrator.fastdream.view.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.fastdream.R;
import com.example.administrator.fastdream.date.bean.BaseBean;
import com.example.administrator.fastdream.date.bean.LoginBean;
import com.example.administrator.fastdream.date.presenter.JPashPresenter;
import com.example.administrator.fastdream.date.presenter.LoginPresenter;
import com.example.administrator.fastdream.date.utils.CacheUtils;
import com.example.administrator.fastdream.date.utils.MyUtils;
import com.example.administrator.fastdream.date.utils.SendSmsTimerUtils;
import com.example.administrator.fastdream.view.base.BaseActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.jpush.android.api.JPushInterface;

public class LoginActivity extends BaseActivity implements LoginPresenter.LoginCallBack, JPashPresenter.DataCallBack {

    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.tv_sendcode)
    TextView tvSendcode;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.tv_login)
    TextView tvLogin;
    private LoginPresenter presenter;
    private JPashPresenter jaashPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        presenter = new LoginPresenter(this, this);
        jaashPresenter = new JPashPresenter(this, this);
    }

    @OnClick({R.id.tv_sendcode, R.id.tv_login})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_sendcode:
                getCode();
                break;
            case R.id.tv_login:
                login();
                break;
        }
    }

    private void getCode() {
        String phone = etPhone.getText().toString().trim();
        if (TextUtils.isEmpty(phone)) {
            Toast.makeText(this, "请输入手机号", Toast.LENGTH_SHORT).show();
            return;
        }
        if (!MyUtils.isMobileNO(phone)) {
            Toast.makeText(this, "请正确格式的手机号", Toast.LENGTH_SHORT).show();
            return;
        }
        presenter.getCode(phone);
        SendSmsTimerUtils.sendSms(tvSendcode, R.color.red, R.color.red);
    }

    private void login() {
        String phone = etPhone.getText().toString().trim();
        String code = etCode.getText().toString().trim();
        if (TextUtils.isEmpty(phone)) {
            Toast.makeText(this, "请输入手机号", Toast.LENGTH_SHORT).show();
            return;
        }
        if (!MyUtils.isMobileNO(phone)) {
            Toast.makeText(this, "请正确格式的手机号", Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(code)) {
            Toast.makeText(this, "请输入验证码", Toast.LENGTH_SHORT).show();
            return;
        }
        presenter.login(phone, code);
    }

    @Override
    public void login(LoginBean loginBean) {
        if (loginBean != null) {
            Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
            CacheUtils.put("token", loginBean.getData().getUser_token().getToken());
            CacheUtils.put("id", loginBean.getData().getUser_token().getId());
            JPushInterface.setAliasAndTags(LoginActivity.this, loginBean.getData().getUser_token().getId(), null, null);
            jaashPresenter.data(loginBean.getData().getUser_token().getToken());
        }
    }

    @Override
    public void date(BaseBean dataBean) {
        finish();
    }
}
